package GUI.Productos;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import article.entities.ArticleVO;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class modificarAtributo extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	ArticleVO art;
	int opcion;
	String mensaje;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public modificarAtributo(Shell parent, int style, ArticleVO a, String m, int op) {
		super(parent, style);
		mensaje=m;
		art=a;
		opcion=op;
		setText("Modifcar Informacion del Producto");
		
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(419, 213);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Label label = new Label(shell, SWT.NONE);
		FormData fd_label = new FormData();
		fd_label.top = new FormAttachment(0, 53);
		label.setLayoutData(fd_label);
		label.setText(mensaje);
		
		text = new Text(shell, SWT.BORDER);
		fd_label.right = new FormAttachment(100, -257);
		FormData fd_text = new FormData();
		fd_text.top = new FormAttachment(label, -3, SWT.TOP);
		fd_text.left = new FormAttachment(label, 37);
		fd_text.right = new FormAttachment(100, -107);
		text.setLayoutData(fd_text);
		
		Button aceptar = new Button(shell, SWT.NONE);
		aceptar.addSelectionListener(new SelectionAdapter() {
			
			public void widgetSelected(SelectionEvent e) {
				if(opcion==0){
					art.setName(text.getText());
					
					
				}
				if(opcion==1){
					art.setPrice(Integer.parseInt(text.getText()));
					VerProductoMod m = new VerProductoMod (shell, shell.getStyle(), art);
					shell.setVisible(false);
					m.open();
				}
				if(opcion==2){
					( art).setDesc(text.getText());
					VerProductoMod m = new VerProductoMod (shell, shell.getStyle(), art);
					shell.setVisible(false);
					m.open();
				}
			}
		});
		FormData fd_aceptar = new FormData();
		fd_aceptar.bottom = new FormAttachment(100, -45);
		aceptar.setLayoutData(fd_aceptar);
		aceptar.setText("Aplicar Cambio");
		
		Button btnCancelar = new Button(shell, SWT.NONE);
		fd_aceptar.left = new FormAttachment(0, 89);
		fd_aceptar.right = new FormAttachment(100, -216);
		FormData fd_btnCancelar = new FormData();
		fd_btnCancelar.right = new FormAttachment(100, -71);
		fd_btnCancelar.left = new FormAttachment(0, 254);
		fd_btnCancelar.bottom = new FormAttachment(100, -45);
		btnCancelar.setLayoutData(fd_btnCancelar);
		btnCancelar.setText("Cancelar");

	}
}
